﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TuongLaiDangCho.DAO;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;

namespace TuongLaiDangCho.Enterface
{
    public partial class ucUpdateChar : UserControl
    {
        QuanLiThoiGianDTO timer = new QuanLiThoiGianDTO();
        public ucUpdateChar()
        {
            InitializeComponent();
            timer.NgayThang = frmHome.choiseDate;
            groUpdateChar.Text = DataProvider.Today(frmHome.choiseDate) + " " + frmHome.choiseDate.ToShortDateString();
        }
        void LoadData()
        {
            gri_col_cmb_Useful.Items.Clear();
            gri_col_cmb_Useful.Items.Add("Helpful");
            gri_col_cmb_Useful.Items.Add("UnHelpful");
            gri_col_cmb_Useful.Items.Add("NoName");
            grcUpdateChar.DataSource = null;
            grcUpdateChar.DataSource = QuanLiThoiGianDAO.LoadDataChar(timer);            
            
        }

        //public void LoadChar()
        //{
        //    if (frmHome.chaThoiGian.Series.Count > 0)
        //    {
        //        chaThoiGian.BeginInit();
        //        Series series;
        //        series = chaThoiGian.Series[0];
        //        series.Points.Clear();
        //        DataTable dt = QuanLiThoiGianDAO.LoadChar();
        //        for (int i = 0; i < 3; i++)
        //        {
        //            series.Points.Add(new SeriesPoint(i + 1, dt.Rows[0][i]));
        //        }
        //        series.Points[0].Argument = "Hữu Ích";
        //        series.Points[1].Argument = "Không Hữu Ích";
        //        series.Points[2].Argument = "Không Tên";
        //        chaThoiGian.EndInit();
        //    }
        //}
        private void ucUpdateChar_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void grv_UpdateChar_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void grv_UpdateChar_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string s_Error = "";
            bool b_Vali = true;
            if (grv_UpdateChar.GetRowCellValue(e.RowHandle, "NoiDung").ToString() == "" 
                || grv_UpdateChar.GetRowCellValue(e.RowHandle, "ThoiGian").ToString() == ""
                || grv_UpdateChar.GetRowCellValue(e.RowHandle, "HuuIch").ToString() == "")
            {
                b_Vali = false;
                s_Error = "Mục này không thể bỏ trống";
                LoadData();
            }
            if (b_Vali)
            {
                if (e.RowHandle < 0)
                {
                    timer.NoiDung = grv_UpdateChar.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    timer.ThoiGian=int.Parse(grv_UpdateChar.GetRowCellValue(e.RowHandle,"ThoiGian").ToString());
                    timer.HuuIch = grv_UpdateChar.GetRowCellValue(e.RowHandle, "HuuIch").ToString();
                    if (timer.HuuIch != "Helpful" && timer.HuuIch != "UnHelpful" && timer.HuuIch != "NoName")
                    {
                        XtraMessageBox.Show("Bạn phải chọn giá trị trong danh sách!" + "\n" + "Nhấp vào mũi tên để sổ ra danh sách", "Error!");
                        LoadData();
                    }
                    else
                    {
                        QuanLiThoiGianDAO.InsertDataChar(timer);
                        LoadData();
                        frmHome.LoadChar();
                    }
                    
                    //XtraMessageBox.Show(grv_UpdateChar.GetRowCellValue(e.RowHandle, gri_col_Useful).ToString(), "Insert");
                }
                else
                {
                    //XtraMessageBox.Show(grv_UpdateChar.GetRowCellValue(e.RowHandle, gri_col_Useful).ToString(),"Update");
                    timer.NoiDung = grv_UpdateChar.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    timer.ThoiGian = int.Parse(grv_UpdateChar.GetRowCellValue(e.RowHandle, "ThoiGian").ToString());
                    timer.HuuIch = grv_UpdateChar.GetRowCellValue(e.RowHandle, "HuuIch").ToString();
                    timer.ID = int.Parse(grv_UpdateChar.GetRowCellValue(e.RowHandle, "ID").ToString());
                    if (timer.HuuIch != "Helpful" && timer.HuuIch != "UnHelpful" && timer.HuuIch != "NoName")
                    {
                        XtraMessageBox.Show("Bạn phải chọn giá trị trong danh sách!" + "\n" + "Nhấp vào mũi tên để sổ ra danh sách", "Error!");
                    }
                    else
                    {
                        QuanLiThoiGianDAO.UpdateDataChar(timer);
                        frmHome.LoadChar();
                    }
                }
            }
            else
            {
                XtraMessageBox.Show(s_Error);
            }
        }

        private void grcUpdateChar_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && grv_UpdateChar.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Are You Sure?", "Quets", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    timer.ID = int.Parse(grv_UpdateChar.GetRowCellValue(grv_UpdateChar.FocusedRowHandle, "ID").ToString());
                    QuanLiThoiGianDAO.DeleteDataChar(timer);
                    LoadData();
                    frmHome.LoadChar();
                }
            }
        }
       
    }
}
